package com.catietie.bookmanagement.controller;

import com.catietie.bookmanagement.common.result.PageResult;
import com.catietie.bookmanagement.common.result.Result;
import com.catietie.bookmanagement.dao.pojo.Book;
import com.catietie.bookmanagement.dto.req.BookListReqDTO;
import com.catietie.bookmanagement.service.BookService;
import com.catietie.bookmanagement.service.Impl.BookServiceImpl;
import com.catietie.bookmanagement.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
@Slf4j
public class BookController {

    @Autowired
    private BookService bookService;

    @Autowired
    private UserService userService;


    /**
     * 分页查询图书
     *
     * @param page
     * @param bookListReqDTO
     * @return
     */
    @GetMapping("/books")
    public PageResult getAllBooks(@RequestParam("page") Integer page, @RequestBody BookListReqDTO bookListReqDTO) {
        return bookService.getAllBooks(page, bookListReqDTO);
    }

    /**
     * 根据bookid借阅书籍
     *
     * @return
     */
    @PostMapping("/book/borrow/{id}")
    public Result borrowBook(@PathVariable Integer bookId) {
        bookService.borrowBook(bookId);
        return Result.success();
    }


    /**
     * 归还书籍
     * @param bookId
     * @return
     */
    @PostMapping("book/return/{id}")
    public Result returnBook(@PathVariable Integer bookId) {
        bookService.returnBook(bookId);
        return Result.success();
    }
//
//    @PostMapping("/borrow/{username}/{bookId}")
//    public void borrowBook(@PathVariable String username, @PathVariable Long bookId) {
//        User user = userService.getUserByUsername(username);
//        if (user == null) {
//            throw new RuntimeException("用户不存在");
//        }
//        bookServiceImpl.borrowBook(user.getId(), bookId);
//    }
//
//    @PostMapping("/return/{bookId}")
//    public void returnBook(@PathVariable Long bookId) {
//        bookServiceImpl.returnBook(bookId);
//    }
}